// pages/market/search-page/search-page.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageNum:1,
    searchKey:"",
    goods_list:[],
    searchKeyList: [],
    searchShow: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getSearchList()
  },
  // 获取搜索记录
  getSearchList() {
    let _this = this;
    app.util.request({
      url: "api/getSearchHistoryList",
      method: "post",
      data: {},
      success(response) {
        if (response.data.code == 0) {
          console.log(response)
          _this.setData({
            searchKeyList: response.data.data,
            searchShow: true
          })
          wx.hideLoading()
        } else {
          _this.setData({
            searchKeyList: []
          })
        }
      }
    })
  },
  onChange(e){
    this.setData({
      searchKey:e.detail
    });
    if (e.detail.length > 0) {
      this.setData({
        searchShow: false
      })
    } else {
      this.setData({
        searchShow: true,
        goods_list: []
      })
      this.getSearchList()
    }
  },
  isNull(){
    return new Promise(resolve => {
      let that = this
      if (that.data.searchKey == "" ) resolve(true) ;
      let regu = "^[ ]+$";
      let re = new RegExp(regu);
      resolve(re.test(that.data.searchKey))
    })
  },
  searchMarket(){
    let _this = this
    _this.isNull().then(res =>{
      if (res){
        wx.showToast({
          icon:'none',
          title:'请输入搜索内容'
        })
      }else{
       _this.data.pageNum=1;
       _this.setData({
        goods_list:[]
       })
       _this.toSearch()
       _this.addSearchKey()
      }
    })
  },
  // 点击搜索记录
  searchKeys(e) {
    console.log(e.currentTarget.dataset.item.content);
    let _this = this;
    _this.setData({
      searchKey: e.currentTarget.dataset.item.content,
      pageNum: 1,
      searchShow: false
    })
    _this.toSearch()
  },
  toSearch(){
    let _this = this
    wx.showLoading({
      title:'加载中'
    })
    app.util.request({
      url: "api/mall/likeMall",
      method: "post",
      data: {
        name:_this.data.searchKey,
        page: _this.data.pageNum,
        pageSize: 10,
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            totalPage: response.data.data.pages,
            goods_list: _this.data.pageNum == 1 ? response.data.data.list : [..._this.data.goods_list, ...response.data.data.list]
          })
          wx.hideLoading()
        }
      }
    })
  },
  // 添加搜索记录
  addSearchKey() {
    let _that = this;
    app.util.request({
      url: "api/saveSearchHistory",
      method: "post",
      data: {
        content: _that.data.searchKey
      },
      success(response) {
        if (response.data.code == 0) {
          _this.setData({
            totalPage: response.data.data.pages
          })
          wx.hideLoading()
        }
      }
    })
  },
  toPersonalGoods(e) {
    wx.navigateTo({
      url: '/pages/goods/goods-detail1/goods-detail1?id=' + e.currentTarget.dataset.id,
    })
  },
  // 移除搜索记录
  removeLocalSearch(e) {
    let _that = this;
    if(_that.data.searchKeyList.length < 1) {
      return
    }
    wx.showModal({
      title: '提示',
      content: '确认删除全部的搜索记录吗？',
      success: (res => {
        if (res.confirm) {
          app.util.request({
            url: "api/removeSearchHistoryAll",
            method: "post",
            data: {},
            success(response) {
              if (response.data.code == 0) {
                wx.showToast({
                  icon: 'success',
                  title: '删除成功！'
                })
                setTimeout(() => {
                  _that.getSearchList()
                })
              } else {
                wx.showToast({
                  title: response.data.message,
                  icon: 'error',
                  duration: 1500
                })
              }
            }
          })
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      pageNum:1
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.pageNum<this.data.pageTotal){
      this.data.pageNum++
      this.toSearch()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
